<?php defined('SYSPATH') or die('No direct script access.');

header("Content-Type:text/html;charset=utf-8");

class Controller_Common extends Controller {
    
    public function action_uploadFile(){

        $name=$this->request->post("name");

        $img_md_strs=$this->request->post("img_md_strs");

        $file=$_FILES[$name];

        $filepath=$file['tmp_name'];

        $img_md_str=md5_file($filepath);
   
        if(empty($img_md_strs)||!strpos($img_md_strs,$img_md_str)){

            $filename=$file['name'];

            if (preg_match("/[\x7f-\xff]/", $filename)){

                $img_name="temp".date('Ymd').rand(1,9999);

            }

            $img_name=date('Ymd').rand(1,9999).$filename;

            $upFilePath ='temp/t/'.$img_name;

            $model_common=new Model_Common();

            $r=$model_common->upload_file($filepath,$upFilePath,'');

            if($r){

                $result['info']=array(
                    'img_src'=>"/".$upFilePath,
                    'img_md_str'=>$img_md_str,
                    'img_name'=>$img_name
                );

                $result['code']='0x00000';
                
            }
            else{

                 $result['result']="对不起，上传失败";
            }
        }
        else{

            $result['result']="对不起，文件已经上传";
        }

        echo json_encode($result);
    }
    public function upload_file($filepath,$upFilePath,$image=''){

        if(file_exists($filepath)){

            $result=move_uploaded_file($filepath,$upFilePath);

        }

        if($result==true){

            if (is_file($image)){

              unlink ($image);

            }

            return true;

        }else{

            return false;

        }
    }
    public function action_make_img(){

        $spu=$this->request->post("spu");

        $img_name=$this->request->post("img");

        try {

            $img = Image::factory($img_name);

            $filename ='temp/images/product/'.$spu.'-M-400400.jpg';

            $img->resize(400, 400)->save($filename);

            $filename ='temp/images/product/'.$spu.'-M-260260.jpg';

            $img->resize(260, 260)->save($filename);

            $filename ='temp/images/product/'.$spu.'-M-100100.jpg';

            $img->resize(100, 100)->save($filename);

            $model=new Model_Admin_Product();

            $sku_info=$model->getSkuInfo($spu);

            if(!empty($sku_info)){

                foreach ($sku_info as $key => $sku) {

                    if(!empty($sku['image'])){

                         $img = Image::factory($sku['image']);

                        $filename ='temp/images/product/'.$sku['sku'].'-M-400400.jpg';

                        $img->resize(400, 400)->save($filename);

                        $filename ='temp/images/product/'.$sku['sku'].'-M-260260.jpg';

                        $img->resize(260, 260)->save($filename);

                        $filename ='temp/images/product/'.$sku['sku'].'-M-100100.jpg';

                        $img->resize(100, 100)->save($filename);   
                    }
                }
            }
            echo json_encode(array("info"=>"success"));
        } 
        catch (Exception $e) {
            echo json_encode(array("info"=>"faild"));
        }
    } 
    public function action_getImage(){

        $type=1;

        $url=$this->request->post("image_url");

        $result=array();

        if($url=='')
        {
            echo json_encode(array('code' => '0x00001','result'=>'url is not exists'));
        }

        $ext=strtolower(strrchr($url,'.'));

        if(!in_array($ext, array('.jpg','.jpeg','.gif','.png','.bmp')))
        {
            echo json_encode(array('code' => '0x00001','result'=>'请输入正确的图片地址'));
        }

        $filename=date('Ymd').rand(1,9999).$ext;
        
        //文件保存路径
        if($type){
            $ch=curl_init();
            $timeout=5;
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
            $img=curl_exec($ch);
            curl_close($ch);
        }else{
            ob_start();
            readfile($url);
            $img=ob_get_contents();
            ob_end_clean();
        }
        $size=strlen($img);

        //文件大小
        $fp2=@fopen('temp/t/'.$filename,'a');
        fwrite($fp2,$img);
        fclose($fp2);

        $imgsrc='/temp/t/'.$filename;

        $img_md_str=md5_file('temp/t/'.$filename);

        echo json_encode(array('code' => '0x00000','result'=>'success','info'=>array("img_name"=>$filename,"img_md_str"=>$img_md_str,"img_src"=>$imgsrc)));
        
    }
}
?>